package com.qys.livesMall.app.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.model.entity.AppUserGoldRecord;
import com.qys.livesMall.app.user.mapper.AppUserGoldRecordMapper;
import com.qys.livesMall.app.user.model.vo.app.AppUserGoldRecordVO;
import com.qys.livesMall.app.user.service.AppUserGoldRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.finance.enums.AmountIconEnum;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.framework.view.QysPage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户金币账户流水表 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-06-24
 */
@Service
public class AppUserGoldRecordServiceImpl extends ServiceImpl<AppUserGoldRecordMapper, AppUserGoldRecord> implements AppUserGoldRecordService {

    @Override
    public QysPage<AppUserGoldRecordVO> page(BaseQuery baseQuery) {
        LambdaQueryWrapper<AppUserGoldRecord> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUserGoldRecord::getUserId,  SecurityUtils.getUserId());
        queryWrapper.orderByDesc(AppUserGoldRecord::getCreateTime);
        Page<AppUserGoldRecord> page=baseMapper.selectPage(new Page<>(baseQuery.getPageNum(), baseQuery.getPageSize()), queryWrapper);
        List<AppUserGoldRecordVO> vos=new ArrayList<>();
        page.getRecords().forEach(e->{
            AppUserGoldRecordVO vo= BeanProUtils.copyProperties(e,AppUserGoldRecordVO::new);
            vo.setUserName(SecurityUtils.getUsername());
            if (e.getRecordType()==1){
                vo.setRecordTypeStr(MessageUtils.message("user.gold.record"));
                vo.setIcon(AmountIconEnum.JH.getUrl());
            }else {
                vo.setRecordTypeStr(MessageUtils.message("user.gold.exchange"));
                vo.setIcon(AmountIconEnum.DH.getUrl());
            }
            vos.add(vo);
        });
        return QysPage.build(baseQuery).setRecords(vos).setTotal(page.getTotal());
    }
}
